// pages/sousuo/sousuo.js
const api = require('../../util')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        oldval: '',
        inpval: '',
        type: true,
        list: [],
        getSearchRecordarr: [],
        ids: []
    },
    //  获取文章列表
    getArticleList(e) {
        var that = this
        this.setData({
            list: []
        })
        var search = this.data.inpval
        if (typeof(e)!='undefined') {
            search = e
        }
        api.http('/article/getArticleList', 'POST', true, {
            category_id: '',
            search
        }, function (res) {
          if(res.code == 200) {
            that.setData({
                list: res.data,
                ids: [],
                type: true
            })
            that.getSearchRecord()
          }
        })
    },
    // 搜索记录
    getSearchRecord() {
        var that = this
        api.http('/article/getSearchRecord', 'POST', false, {
        }, function (res) {
          if(res.code == 200) {
            that.setData({
                getSearchRecordarr: res.data
            })
          }
        })
    },
    // 点击搜索
    getinp(e) {
        this.setData({
            ids: [],
            inpval: e.currentTarget.dataset.value,
            oldval: e.currentTarget.dataset.value
        })
        this.getArticleList(e.currentTarget.dataset.value)
    },
    // 删除单个历史
    delSearchRecord(e,index) {
        var arr = this.data.getSearchRecordarr
        arr.splice(e.currentTarget.dataset.index,1)
        var ids = this.data.ids
        ids.push(e.currentTarget.dataset.id)
        this.setData({
            ids,
            getSearchRecordarr: arr
        })
    },
    // 保存
    bc(){
        var that = this,ids=this.data.ids.toString()
        if (ids=='') {
            this.setData({
                type: !that.data.type
            }) 
            return
        }
        api.http('/article/delSearchRecord', 'POST', true, {
            ids
        }, function (res) {
          if(res.code == 200) {
            wx.showToast({
                title: res.msg,
                icon: 'success',
                duration: 1000
            })
            that.typeshow()
          } else {
            wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 1000
            }) 
          }
        })
    },
    inputHandler(e) {
        this.setData({
            oldval: e.detail.value
        })
        if (e.detail.value=='') {
            this.setData({
                inpval: e.detail.value
            }, () => {
                // this.getArticleList()
            })  
        }
    },
    // 输入框回车触发
    confirmTap() {
        var that = this
        this.setData({
            inpval: that.data.oldval
        }, () => {
            that.getArticleList()
        })
    },
    // 取消搜索
    gbfun() {
        this.setData({
            inpval: '',
            oldval: ''
        }, () => {
            this.getArticleList()
        }) 
    },
    typeshow() {
        var that = this
        if (this.data.getSearchRecordarr.length == 0) {
            return
        }
        this.setData({
            type: !that.data.type
        }, () => {
            if (that.data.type==true) {
                that.getSearchRecord()
            }
        })
    },
    // 删除触发
    delindex(e) {
        var that = this
        api.http('/article/delAllSearchRecord', 'POST', true, {
        }, function (res) {
          if(res.code == 200) {
            wx.showToast({
                title: res.msg,
                icon: 'success',
                duration: 1000
            })
            that.typeshow()
          } else {
            wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 1000
            }) 
          }
        })
    },
    // 查询
    // getinp() {
    //     var that = this
    //     this.setData({
    //         type: true
    //     }) 
    // },
    // 跳转推荐详情
    toarticledetails(e) {
        wx.navigateTo({
            url: '/pages/articledetails/articledetails?id='+e.currentTarget.dataset.id
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this
        if (typeof(options.inptext)!='undefined') {
            this.setData({
                inpval: options.inptext,
                oldval: options.inptext
            }, () => {
                that.getArticleList()
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage() {

    // }
})